/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo3;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import Modelo.DAL.Ast_DAO;
import Modelo.Ast;
import Acceso.Security;
import Modelo.PuestoTrabajo;
import java.io.PrintWriter;
import org.json.JSONObject;
import org.json.JSONArray;

/**
 *
 * @author Francis
 */
public class listarASTAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String[] ALLOWED = {"Administrador","Gestionar Analisis de Seguridad"};

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        String nacionalidad = request.getParameter("nacionalidad");
        String ci = request.getParameter("ci");
        String realizadoPor = request.getParameter("realizadoPor");
        String fecha = request.getParameter("fecha");
        String fechaModificacion = request.getParameter("fechaModificacion");

        String cargo = request.getParameter("cargo");
        String puesto = request.getParameter("puesto_trabajo");
        String sede = request.getParameter("sede");

        String limit = (String) request.getParameter("limit");
        String offset = (String) request.getParameter("offset");
        String order = (String) request.getParameter("order");
        String asc = (String) request.getParameter("asc");

        Ast ast = new Ast();
        ast.setCi(nacionalidad + "-" + ci);
        ast.setFechaAst(fecha);
        ast.setFechaModificacion(fechaModificacion);
        ast.setRealizadoPor(realizadoPor);

        PuestoTrabajo pt = new PuestoTrabajo();
        pt.setNombreCargo(cargo);
        pt.setNombrePuesto(puesto);
        pt.setSede(sede);

        System.out.println("cici " + ast.getCi());
        response.setContentType("text/json");

        JSONObject jo = Ast_DAO.listarAst(ast, pt, limit, offset, order, asc);

        PrintWriter pw = response.getWriter();
        pw.write(jo.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}
